#include "head_files.h"
#include "uartToPC.h"

uint8_t usart2_rx_buffer[100];    // USART2 receive buffer (100 bytes)
volatile uint8_t usart2_rx_len = 0; // Length of received data, updated by ISR
volatile uint8_t usart2_recv_end_flag = 0; // Receive completion flag

HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size);

void uartToPC_receive_itHandler(uint8_t* rc_buffer)
{
	HAL_UART_IRQHandler(&huart2);
	uint8_t tmp_flag = __HAL_UART_GET_FLAG(&huart2, UART_FLAG_IDLE); // Get IDLE flag status
  if (tmp_flag != RESET) // Check if reception is complete via IDLE flag
  { 
      usart2_recv_end_flag = 1; // Set reception completion flag
      __HAL_UART_CLEAR_IDLEFLAG(&huart2); // Clear IDLE flag
      HAL_UART_DMAStop(&huart2); 
      uint8_t temp = __HAL_DMA_GET_COUNTER(&hdma_usart2_rx);                 
      usart2_rx_len = 100 - temp; // Calculate received data length
      HAL_UART_Transmit_DMA(&huart2, rc_buffer, usart2_rx_len); // Transmit received data
      HAL_UART_Receive_DMA(&huart2, rc_buffer, 100); // Restart DMA reception for next cycle
   }
}

void uart_transmit_float(float data, int width, int prec)
{
	uint8_t buffer[10];
	dtostrf(data,width,prec,buffer);
	HAL_UART_Transmit_DMA(&huart2, buffer, sizeof(buffer));
}

void uart_transmit_int(int data)
{
	//have bug
	uint8_t buffer[10];
	inttostr(data,buffer);
	HAL_UART_Transmit_DMA(&huart2, buffer, sizeof(buffer));
}

uint8_t* inttostr(int num, uint8_t* str)
{
    uint8_t* p = str;
    int isNegative = 0;
    uint32_t abs_value;

    if (num < 0) {
        isNegative = 1;
        abs_value = (uint32_t)(-(int64_t)num);
    } else {
        abs_value = num;
    }

    if (abs_value == 0) {
        *p++ = '0';
        *p = '\0';
        return str;
    }

    uint8_t temp[11];
    uint8_t* q = temp;

    while (abs_value > 0) {
        *q++ = (abs_value % 10) + '0';
        abs_value /= 10;
    }

    if (isNegative) {
        *q++ = '-';
    }

    while (q > temp) {
        *p++ = *--q;
    }

    *p = '\0';

    return str;
}

uint8_t* dtostrf(double value, int width, int prec, uint8_t* str)
{
    uint8_t* buffer = str;
    uint8_t* p = buffer;
    uint8_t* p1;
    uint8_t c;
    unsigned long int_part;
    int frac_part;
    int i;
    int padding;
    
    if (value < 0) {
        *buffer++ = '-';
        value = -value;
    }
    
    if (value < 1e-9 && value > -1e-9) {
        *buffer++ = '0';
        *buffer = '\0';
        return str;
    }
    
    int_part = (unsigned long)value;
    value -= int_part;
    
    frac_part = 0;
    int power = 1;
    for (i = 0; i < prec; i++) {
        value *= 10;
        power *= 10;
    }
    frac_part = (int)(value + 0.5);
    
    if (frac_part >= power) {
        int_part++;
        frac_part = 0;
    }
    
    p = buffer;
    if (int_part == 0) {
        *p++ = '0';
    } else {
        while (int_part > 0) {
            *p++ = (int_part % 10) + '0';
            int_part /= 10;
        }
    }
    
    p1 = p - 1;
    while (buffer < p1) {
        c = *buffer;
        *buffer = *p1;
        *p1 = c;
        buffer++;
        p1--;
    }
    
    if (prec > 0) {
        *p++ = '.';
        
        for (i = power / 10; i > 0; i /= 10) {
            *p++ = ((frac_part / i) % 10) + '0';
        }
    }
    
    padding = width - (p - str);
    
    if (padding > 0) {
        uint8_t* src = p - 1;
        uint8_t* dest = src + padding;
        while (src >= str) {
            *dest-- = *src--;
        }
        
        for (i = 0; i < padding; i++) {
            str[i] = ' ';
        }
        
        p += padding;
    }
    
    *p = '\0';
    return str;
}

int my_strlen(uint8_t* buffer)
{
    int length = 0;

    while (*buffer != '\0')
			{
				length++;
        buffer++;
      }
    return length;
}
